package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private TokenDecode tokenDecode;
    /**
     * 添加购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/add")
    public Result add(@RequestParam("skuId") String skuId,@RequestParam("num") Integer num){


        //动态获取用户名
        String username = tokenDecode.getUserInfo().get("username");
        cartService.add(skuId,num,username);

        return new Result(true, StatusCode.OK,"加入购物车成功");

    }


    /**
     * 查询购物车列表
     * @return
     */
    @GetMapping("/list")
    public Map list(){

        //动态获取用户名
        String username = tokenDecode.getUserInfo().get("username");
        return cartService.list(username);
    }
}
